package cn.mw.cmdb.entity;

import cn.mw.microMonitorCommon.entity.userDTO.CurrentGroup;
import cn.mw.microMonitorCommon.security.dto.MwUser;
import cn.mw.microMonitorCommon.security.utils.AuthUtils;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import cn.mw.microMonitorCommon.util.SpringUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class InputDataAuth {
    public static final String OrgKey = "orgs";
    public static final String OrgIdsKey = "orgIds";
    public static final String GroupKey = "groups";
    public static final String UserKey = "users";
    public static final String OrgNameKey = "orgNames";
    public static final String GroupNameKey = "groupNames";
    public static final String UserNameKey = "userNames";
    public static final String DataPermKey = "dataPerm";

    private List<Long> orgs;
    private List<Long> groups;
    private List<Long> users;
    private Map<String, Object> supplierMap = new HashMap<>();

    public InputDataAuth() {
        MwUser mwUser = SecurityUtils.getUser();
        if (null != mwUser) {
            this.orgs = mwUser.getCurrentOrgId();
            this.supplierMap.put(OrgKey, orgs);
            List<Long> groupDTOS = mwUser.getGroupIdList();
            if (null != groupDTOS) {
                this.groups = groupDTOS;
                this.supplierMap.put(GroupKey, groups);
            }
            this.users = new ArrayList<>();
            if (SecurityUtils.getUserId() != null) {
                users.add(SecurityUtils.getUserId());
            }
            this.supplierMap.put(UserKey, users);
            this.supplierMap.put(DataPermKey, mwUser.getDataPerm());
        }
    }

    public List getAuthData(String key) {
        return (List) supplierMap.get(key);
    }

    public String getDataPerm() {
        return null != supplierMap.get(DataPermKey) ? supplierMap.get(DataPermKey).toString() : "";
    }
}
